package com.skyworthclub.competition.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.skyworthclub.competition.beans.Feedback;
import com.skyworthclub.competition.beans.Match;
import com.skyworthclub.competition.beans.dto.CheckDto;
import com.skyworthclub.competition.service.AdminService;
import com.skyworthclub.competition.service.FeedbackService;
import com.skyworthclub.competition.utils.web.DataValidateUtil;
import com.skyworthclub.competition.utils.web.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author li
 * @version 1.0
 * @since 18-10-26 上午11:07
 **/
@Controller
@RequestMapping(value = "/api/admin")
public class AdminController {

    private AdminService adminService;
    private final FeedbackService feedbackService;

    @Autowired
    public AdminController(AdminService adminService, FeedbackService feedbackService) {
        this.adminService = adminService;
        this.feedbackService = feedbackService;
    }

    @GetMapping("/checkMatch/{page}")
    public ResponseEntity<Map<String, Object>> checkMatch(@PathVariable Integer page) {
        Page<Match> matchPage = adminService.checkMatch(page);
        Map<String, Object> map = new HashMap<>(4);
        map.put("matchPage", matchPage);
        return ResponseUtil.querySuccessResponse(map);
    }

    @PostMapping("/passChecking")
    public ResponseEntity<Map<String, Object>> passChecking(@RequestBody @Valid CheckDto checkDto,
                                                            Errors errors) {

        //数据校验
        ResponseEntity<Map<String, Object>> responseEntity = DataValidateUtil.validate(errors);
        if (responseEntity != null) {
            return responseEntity;
        }

        boolean b = adminService.passChecking(checkDto.getMatchId(), checkDto.getPublisher(), checkDto.getMatchName());
        Map<String, Object> map = new HashMap<>(4);
        if (b) {
            return ResponseUtil.updateSuccessResponse(map);
        } else {
            return ResponseUtil.updateFailureResponse(map);
        }
    }

    @PostMapping("/unPassChecking")
    public ResponseEntity<Map<String, Object>> unPassChecking(@RequestBody @Valid CheckDto checkDto,
                                                              Errors errors) {
        Map<String, Object> map = new HashMap<>(4);

        //数据校验
        ResponseEntity<Map<String, Object>> responseEntity = DataValidateUtil.validate(errors);
        if (responseEntity != null) {
            return responseEntity;
        } else if (checkDto.getMessage() == null || "".equals(checkDto.getMessage().trim())) {
            map.put("error", "请认真填写审核未通过的原因！");
            return ResponseUtil.updateFailureResponse(map);
        }

        boolean b = adminService.unPassChecking(checkDto.getMatchId(), checkDto.getPublisher(), checkDto.getMatchName(),checkDto.getMessage());
        if (b) {
            return ResponseUtil.updateSuccessResponse(map);
        } else {
            return ResponseUtil.updateFailureResponse(map);
        }
    }

    @GetMapping("/checkFeedback/{pageNum}")
    public ResponseEntity<Map<String, Object>> checkFeedBack(@PathVariable(value = "pageNum") Integer pageNum) {

        Map<String, Object> map = new HashMap<>(4);
        Page<Feedback> page = feedbackService.selectFeedbackPage(pageNum);
        map.put("feedbackPage", page);
        return ResponseUtil.querySuccessResponse(map);
    }
}
